package com.vercon.moensmart.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.vercon.common.Constants
import com.vercon.moensmart.databinding.FragmentDemistLayoutBinding
import com.vercon.moensmart.event.BaseEvent
import com.vercon.moensmart.fragment.base.BaseFragment
import com.vercon.moensmart.fragment.manager.FragmentOpenManager
import com.vercon.moensmart.viewmodel.SystemViewModel
import org.greenrobot.eventbus.EventBus

class DemistFragment : BaseFragment<FragmentDemistLayoutBinding?>() {

    private val minList: ArrayList<String> = ArrayList()
    private val hourList: ArrayList<String> = ArrayList()
    var systemViewModel : SystemViewModel? = null

    override fun onCreateViewBinding(inflater: LayoutInflater, parent: ViewGroup?): FragmentDemistLayoutBinding {
        return FragmentDemistLayoutBinding.inflate(inflater, parent, false)
    }

    override fun initData() {
        //滚轮
        for (i in 0..59) {
            if (i < 10) {
                minList.add("0$i")
                hourList.add("0$i")
            } else {
                minList.add("" + i)
                hourList.add("" + i)
            }
        }
        binding?.wheelviewMin?.setData(minList)
        binding?.wheelviewHour?.setData(hourList)

        systemViewModel = ViewModelProvider(requireActivity())[SystemViewModel::class.java]
        systemViewModel?.isOpenDemist?.observe(this@DemistFragment) {
            binding?.igDemistState?.isSelected = it
            binding?.igSwitch?.isSelected = it
            binding?.tvSwitchState?.text =  if (it) "ON" else "OFF"
            if (it) {
                binding?.llTimeLabel?.visibility = View.VISIBLE
                binding?.vTouch?.visibility = View.VISIBLE
                binding?.wheelviewMin?.selectedTextColor = resources.getColor(com.vercon.common.R.color.color_4D9DB4)
                binding?.wheelviewHour?.selectedTextColor = resources.getColor(com.vercon.common.R.color.color_4D9DB4)
            } else {
                binding?.llTimeLabel?.visibility = View.GONE
                binding?.vTouch?.visibility = View.GONE
                binding?.wheelviewMin?.selectedTextColor = resources.getColor(com.vercon.common.R.color.color_A8AAB8)
                binding?.wheelviewHour?.selectedTextColor = resources.getColor(com.vercon.common.R.color.color_A8AAB8)
            }
        }

        systemViewModel?.demistTimeLiveData?.observe(this@DemistFragment){
            binding?.wheelviewMin?.setSelectedPosition(it % 60)
            binding?.wheelviewHour?.setSelectedPosition(it / 60)
        }

    }
    override fun initListener() {
        binding?.igSwitch?.setOnClickListener {
            binding?.igSwitch?.isSelected = !binding?.igSwitch?.isSelected!!
            EventBus.getDefault().post(BaseEvent(Constants.refreshDemist,binding?.igSwitch?.isSelected!!))
            var demist : Int? = binding?.wheelviewMin?.getSelectedPosition()?.plus(binding?.wheelviewHour?.getSelectedPosition()?.times(60) ?: 0)
            if (demist != null) {
                systemViewModel?.setIsOpenDemist(binding?.igSwitch?.isSelected,demist)
            }
        }

       binding?.igClose?.setOnClickListener { v ->
           activity?.let { FragmentOpenManager.getInstance().closeFragment(it) }
        }
        binding?.rlOutsideView?.setOnClickListener {
            activity?.let { FragmentOpenManager.getInstance().closeFragment(it) }
        }
    }

    override fun onClick(v: View?) {}
}
